package com.pictureBook.merchant.controller;


import com.alibaba.fastjson.JSONObject;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.pictureBook.merchant.entity.StoreVipSetmeal;
import com.pictureBook.merchant.service.IStoreVipSetmealService;
import com.pictureBook.merchant.service.impl.StoreVipSetmealServiceImpl;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author Ding
 * @since 2022-01-26
 */
@Api("借阅计划-会员套餐-控制器")
@RestController
@RequestMapping("/lendPlan")
public class StoreVipSetmealController {

    @Resource
    private IStoreVipSetmealService storeVipSetmealService;

    /**
     * 获取节约计划列表
     * @param soId 商户id
     * @param index 页码
     * @param num 每页显示数
     * @param key 检索词-byName
     * @return
     */
    @ApiOperation("获取借阅计划列表")
    @ResponseBody
    @GetMapping("/getList")
    public JSONObject getList(
            @RequestParam("so_id") Integer soId,
            @RequestParam("index") Integer index,
            @RequestParam("num") Integer num,
            @RequestParam(value = "key",defaultValue = "") String key){
        JSONObject jsonObject = new JSONObject();
        JSONObject jsonTemp = new JSONObject();
        PageInfo<StoreVipSetmeal> list = new PageInfo<>();
        try {
            list = storeVipSetmealService.getList(soId,key,index,num);
            jsonObject.put("code",200);
            jsonObject.put("msg","获取成功");
            jsonTemp.put("total",list.getTotal());
            jsonTemp.put("index",list.getPageNum());
            jsonTemp.put("list",list.getList());
            jsonObject.put("data",jsonTemp);
        }catch (Exception e){
            e.printStackTrace();
            jsonObject.put("code",500);
            jsonObject.put("msg","服务器异常，获取失败");
        }
        return jsonObject;
    }

    /**
     * 添加/编辑 借阅计划
     * @param bean 封装成实体类
     * @return json
     */
    @ApiOperation("添加/编辑借阅计划")
    @ResponseBody
    @PostMapping("/editLendPlan")
    public JSONObject addLendPlan(@RequestBody StoreVipSetmeal bean){
        JSONObject jsonObject = new JSONObject();
        int adds = 0;
        try {
            if (bean.getSoVSetmeauId() == null){
                //添加
                adds += storeVipSetmealService.addLendPlan(bean);
            }else{
                //编辑
                storeVipSetmealService.editLendPlan(bean);
                adds ++;
            }
            if (adds > 0){
                jsonObject.put("code",200);
                jsonObject.put("msg",bean.getSoVSetmeauId()==null?"添加":"编辑"+"成功");
            }else{
                jsonObject.put("code",500);
                jsonObject.put("msg","服务器异常，添加失败");
            }
        }catch (Exception e){
            e.printStackTrace();
            jsonObject.put("code",500);
            jsonObject.put("msg","服务器异常，添加失败");
        }
        return jsonObject;
    }

    /**
     * 删除节约计划
     * @param data 传过来的 k-v 值
     * @return json
     */
    @ApiOperation("删除借阅计划（套餐）")
    @ResponseBody
    @PostMapping("/deleteLendPlan")
    public JSONObject deleteLendPlan(@RequestBody JSONObject data){
        JSONObject jsonObject = new JSONObject();
        int dels = 0;
        try {
            List<Integer> ids = (List<Integer>) data.get("ids");
            dels += storeVipSetmealService.deleteLendPlan(ids);
            if (dels >= ids.size()){
                jsonObject.put("code",200);
                jsonObject.put("msg",dels+"条数据全部删除成功");
            }else{
                jsonObject.put("code",510);
                jsonObject.put("msg",dels==0?"服务器异常，删除失败":"部分删除成功："+dels+"/"+ids.size());
            }
        }catch (Exception e){
            e.printStackTrace();
            jsonObject.put("code",500);
            jsonObject.put("msg","服务器异常，删除失败");
        }

        return jsonObject;
    }
}
